package Kuang;

/**
 * description:
 * author:zcx
 * lenovo
 * 时间：2022.02.09.20.06
 */
public class Demo13 {
    public static void main(String[] args) {
    var by1=new Byticke();
        new Thread(by1,"张同学").start();
        new Thread(by1,"黄牛党").start();
        new Thread(by1,"王老师").start();
        new Thread(by1,"张").start();
        new Thread(by1,"黄").start();
        new Thread(by1,"王").start();
    }
}
class Byticke implements Runnable {
    Object obj = new Object();
    int Ticket = 10;
    Boolean flag = true;

    //买票
    @Override

    public void run() {

            while (flag) {
                by();
            }

        }

        public synchronized void by () {

                if (Ticket <= 0) {
                    flag = false;
                    return;
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "买到了第" + Ticket-- + "票");
            }

    }

